package com.lyh.controller;

import com.lyh.pojo.PersonAndUser;
import com.lyh.pojo.User;
import com.lyh.service.TicketService;
import com.lyh.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * 详情页面的接口
 */
@Controller
@RequestMapping("/detail")
public class DetailController {

    @Autowired
    UserService userService;

    @Autowired
    TicketService ticketService;

    @RequestMapping("/page/{infoType}")
    String userDetail(HttpSession session, Model model, @PathVariable("infoType") String infoTYpe){
        //拿出session中登陆的账号信息,然后展示到页面上
        User user =  (User) SecurityUtils.getSubject().getPrincipal();
        //使用用户名获取用户的所有详细信息, 并装入model中, 展示在个人页面
        PersonAndUser result = userService.getUserDetail(user.getUsername());
        session.setAttribute("userDetail",result);
        //设置type, 是因为其他按钮直接跳转到相应的详细面板
        model.addAttribute("infoType",infoTYpe);
        return "detail";
    }

    /**
     * 详情页面, 右边的页面切换
     * @param type
     */
    @RequestMapping("/type/{type}")
    String changeInfo(@PathVariable("type")String type){
        //用于信息页面ajax的碎片切换
        String prefix = "components/detail/details::";
        switch (type){
            case "1": return prefix + "userInfo";
            case "2": return prefix+"accountSecurity";
            case "3": return prefix+"orderDetail";
            default: return "detail";
        }
    }

    /**
     * 订单详情页面, 切换
     * @param type 指定切换的类型
     */
    @RequestMapping("/order/type/{type}")
    String changeOrder(@PathVariable("type")String type,Model model){
        //用于信息页面ajax的碎片切换
        String prefix = "components/detail/orderPage::";
        model.addAttribute("type",type);//保存类型到html中, 在html中再ajax获取

        //一开始想切换碎片, 但是发现都展示票页面都一样, 只是请求的票状态不一样
        switch (type){
            case "unpaid":
                return prefix + "orderList";
            case "paid":
                return prefix + "orderList";
            case "refund":
                return prefix + "orderList";
            case "finish":
                return prefix + "orderList";
            case "all":
                return prefix + "orderList";
            default:
                model.addAttribute("type","nothing");
                return prefix + "nothing";
        }

    }




}
